package cn.bugstack.infrastructure.gateway;

import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * @Author: 代码的路
 * @Date: 2025/3/6 15:30
 * @Param:
 * @Return:
 * @Description:
 **/
public class TokenHeaderInterceptor implements Interceptor {


    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        String token  ="12312412412"; //SpUtils是SharedPreferences的工具类，自行实现
        if (token.isEmpty()) {
            Request originalRequest = chain.request();
            return chain.proceed(originalRequest);
        }else {
            Request originalRequest = chain.request();
            //key的话以后台给的为准，我这边是叫token
            Request updateRequest = originalRequest.newBuilder().header("token", token).build();
            return chain.proceed(updateRequest);
        }

    }

    /**
     * 原生http请求
     */
    private Response getToken(String url) throws Exception {
        //1 构造Request
        OkHttpClient okHttpClient = new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        Request request = builder.get().url(url).build();
        //2 将Request封装为Call
        Call call = okHttpClient.newCall(request);
        //3 执行Call，得到response
        return call.execute();
    }

}
